package DecertifiSys.controller;

import DecertifiSys.service.AccountImpl;
import DecertifiSys.service.TokenImpl;
import DecertifiSys.untill.baseUntill.HttpDataUntill;
import DecertifiSys.untill.baseUntill.MessageUntill;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by zhoumeineng on 2018/2/1.
 */
@RestController
@RequestMapping(value = "/decetificSys/${app.version}",method = RequestMethod.POST)
public class DecetifiSysController {

    @Autowired
    TokenImpl tokenImpl;


    @Autowired
    AccountImpl accountImpl;



    /**
     *  验证token
     *  0.1 版本号
     *  url:http://DECETIFICSYS/0.1/authToken
     * */

    @RequestMapping(value = "/authToken")
    public void authToken(HttpServletRequest req, HttpServletResponse rsp){
      JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
      JSONObject jsonObject =  tokenImpl.autnToken(result.getString("uid"),result.getString("token"));
      MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


    /**
     *
     * 生成交易Token
     *
     * */
    @RequestMapping("/generationgToken")
    public void generationgToken(HttpServletRequest req, HttpServletResponse rsp){
        JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
        JSONObject jsonObject = tokenImpl.bringToken(result.getString("logintoken"));
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


    /**
     *
     * 注销token
     * */
    @RequestMapping("/cancelToken")
    public void cancelToken(HttpServletRequest req, HttpServletResponse rsp){
        JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
        JSONObject jsonObject = tokenImpl.cancelToken(result.getString("uid"));
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


    /**
     * 账号登录
     * */
     @RequestMapping("/accountLogin")
    public void accountLogin(HttpServletRequest req, HttpServletResponse rsp){

      /**
       * 获取前端传递的参数
       * */
      JSONObject result1 = HttpDataUntill.httpRspSet(req,rsp);

      /**
       * 登录 验证账号密码
       * */
      JSONObject jsonObject =  accountImpl.AccounLogin(result1);
      MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


    /**
     *
     * 刷脸登录 获取脸部照片
     * */

    @RequestMapping("/getFaceImage")
    public void getFaceImage(HttpServletRequest req, HttpServletResponse rsp){
        /**
         * 获取前端传递的参数
         * */
        JSONObject result1 = HttpDataUntill.httpRspSet(req,rsp);
        MessageUntill.ResboneOutPut(rsp,accountImpl.getFaceImage(result1));
    }



    @RequestMapping("/faceLogin")
    public void faceLogin(HttpServletRequest req, HttpServletResponse rsp){
        JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
        MessageUntill.ResboneOutPut(rsp,accountImpl.faceLogin(result.getString("phoneNum"),req));
    }


    @RequestMapping("/loginOut")
    public void loginOut(HttpServletRequest req, HttpServletResponse rsp){
        JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
        JSONObject jsonObject = tokenImpl.cancelToken(result.getString("uid"));
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }

}
